package com.abxh.dawdler

import android.annotation.SuppressLint
import android.content.Intent
import android.os.Bundle
import android.util.Log
import androidx.activity.compose.setContent
import androidx.compose.foundation.Image
import androidx.compose.foundation.clickable
import androidx.compose.foundation.layout.*
import androidx.compose.material.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.ImageBitmap
import androidx.compose.ui.res.painterResource
import androidx.compose.ui.text.style.TextAlign
import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.ui.unit.dp
import androidx.lifecycle.viewmodel.compose.viewModel
import com.abxh.core.MmkvHelper
import com.abxh.core.utils.LogUtils
import com.abxh.dawdler.cat.catList
import com.abxh.dawdler.viewmodel.ViewModelMain
import com.abxh.ffmpage.FFmpageActivity
import com.abxh.jetpack.base.BaseViewModelActivity

open class MainActivity : BaseViewModelActivity<ViewModelMain>() {


    @SuppressLint("MissingSuperCall")
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        LogUtils.setShowLog(true)
        setContent {
            PreviewCounter()
        }
    }

    @Preview
    @Composable
    fun PreviewCounter() {
        val padding = 16.dp
        Column(
            modifier = Modifier
                .padding(padding)
                .fillMaxWidth()
                .fillMaxHeight()
        ) {
            add()
            Text(
                text = "roomTest",
                modifier = Modifier
                    .fillMaxWidth()
                    .height(20.dp)
                    .clickable {
                        this@MainActivity.startActivityForResult(
                            Intent(this@MainActivity, FFmpageActivity::class.java), 0x187
                        )
                    },
                textAlign = TextAlign.Center
            )
            catList()
        }

    }


    @Composable
    private fun add() {
        var times = remember {
            mutableStateOf(MmkvHelper.getInstance().getInt("key", 0))
        }
        Text(
            text = times.value.toString(),
            modifier = Modifier
                .height(20.dp)
                .fillMaxWidth()
                .clickable {
                    times.value = times.value + 1
                    MmkvHelper
                        .getInstance()
                        .putInt("key", times.value)
                },
            textAlign = TextAlign.Center
        )
    }

    override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
        super.onActivityResult(requestCode, resultCode, data)
        if (requestCode == 0x187 && resultCode == RESULT_OK) {
            val stringExtra = data?.getStringExtra("url")

        }
    }

}